﻿import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Controls.impl 2.14
import QtGraphicalEffects 1.14

Item {

    id: root

    property var model: []
    property var modell: []

    function setValue(value){
        for(let i = 0; i < model.length; i ++){
            if(model[i] === value){
                itemIdx = i
                item = value
            }
        }
    }

    property bool mousePressed: false

    onVisibleChanged: {
        if(visible){
            listView.contentY = (itemIdx) * itemHeight + listView.originY
        }
    }

    onModelChanged: {
        supModel()
    }Component.onCompleted: {
        supModel()
    }

    function supModel(){
        modell = ["", ""]
        for(let i = 0; i < model.length; i ++){
            modell.push(model[i])
        }modell.push("")
        modell.push("")
        modellChanged()
    }

    property int itemHeight: 30*dp

    property int itemIdx: 0
    property int item: 0
    property string text: item>=10 ? item.toString() : "0"+item.toString()


    ListView {
        id: listView
        anchors.fill: parent
        model: root.modell.length
        delegate: Rectangle{
            height: itemHeight
            width: parent.width
            property int modelIdx: index
            Text {
                anchors.centerIn: parent
                font.pixelSize: config.fontsize
                text: root.modell[index]
            }
            MouseArea{
                anchors.fill: parent
                cursorShape: Qt.PointingHandCursor
                onClicked: {
//                    console.log(parent.modelIdx)
                    itemIdx = parent.modelIdx
                    listView.contentY = (itemIdx-2) * itemHeight + listView.originY
                    item = root.model[itemIdx-2]
                }
            }
        }
        onContentYChanged: {
            if(!root.visible){
                return
            }

            if(timerRunning){
                return
            }
            lastContentY = contentY - originY
            timer.start()
            timerRunning = true
        }
    }

    property bool timerRunning: false

    // 监听ContentY变化事件是否已经结束
    property real lastContentY
    Timer{
        id: timer
        interval: 50
        repeat: true
        onTriggered: {
            if(listView.contentY-listView.originY < 0 || listView.contentY-listView.originY > itemHeight * (modell.length-5)){
//                console.log("超出范围不进行校验", 0, listView.contentY-listView.originY, itemHeight*(modell.length-5))
                return
            }

            if(mousePressed){
                lastContentY = listView.contentY-listView.originY
                return
            }

            if(Math.abs(lastContentY-(listView.contentY-listView.originY)) < 1){
                timer.stop()
                timerRunning = false
                itemIdx = Math.round((listView.contentY-listView.originY) / itemHeight)
                listView.contentY = itemIdx * itemHeight + listView.originY
                item = model[itemIdx]
            }else{
//                console.log("Last", lastContentY, "Now", listView.contentY-listView.originY)
            }


            lastContentY = listView.contentY-listView.originY
        }
    }

    // 遮罩
    Rectangle{
        height: (root.height-itemHeight) / 2
        width: root.width
        opacity: 0.7
    }Rectangle{
        y: root.height/2 + itemHeight / 2
        height: (root.height-itemHeight) / 2
        width: root.width
        opacity: 0.7
    }



}
